package co.infinum.goldfinger.example;

import co.infinum.goldfinger.Goldfinger;
import co.infinum.goldfinger.example.widget.ProgressDialog;
import co.infinum.goldfinger.example.widget.SwitchCompat;
import co.infinum.goldfinger.util.LogUtils;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
import ohos.agp.components.TextField;
import ohos.agp.utils.TextTool;

/**
 * ex.
 *
 * @author zh
 * @since 2021-06-25
 */
public abstract class BaseSettingsAbility extends Ability {
    private boolean mIsChecked;
    private ProgressDialog dialog;
    private Text currentPinView;
    private TextField pinInputView;
    private Component changePinButton;
    private SwitchCompat fingerprintSwitchView;
    private Component logoutButton;
    private Text tvResult;

    protected abstract void initGoldfinger();

    protected abstract void encryptFingerprintPin(String pin);

    protected void handleGoldfingerError(Throwable e) {
        getUITaskDispatcher().asyncDispatch(new Runnable() {
            @Override
            public void run() {
                dismissDialog();
                fingerprintSwitchView.setChecked(false);
                tvResult.setText(e.getMessage());
            }
        });
    }

    protected void handleGoldfingerResult(Goldfinger.Result result) {
        getUITaskDispatcher().asyncDispatch(new Runnable() {
            @Override
            public void run() {
                dismissDialog();
                if (result.type() == Goldfinger.Type.SUCCESS) {
                    SharedPrefs.setFingerprintPin(result.value());
                } else if (result.type() == Goldfinger.Type.ERROR) {
                    fingerprintSwitchView.setChecked(false);
                } else {
                    LogUtils.isLogEnable();
                }
                tvResult.setText(result.message());
            }
        });
    }

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_settings);
        initGoldfinger();
        initViews();
        initListeners();
    }

    private void activateFingerprint() {
        String pin = SharedPrefs.getPin();
        if (pin == null) {
            return;
        }
        encryptFingerprintPin(pin);
    }

    private void initListeners() {
        pinInputView.addTextObserver(new SimpleTextWatcher() {
            @Override
            public void onTextChanged(String text) {
                if (TextTool.isNullOrEmpty(text)) {
                    return;
                }
                String newStr = text;
                newStr = newStr.replaceAll("[^0-9]", "");
                if (text.length() > 4) {
                    newStr = text.substring(0, 4);
                }
                pinInputView.setText(newStr);
                changePinButton.setEnabled(newStr.length() == 4);
            }
        });
        logoutButton.setClickedListener(v -> terminateAbility());
        fingerprintSwitchView.setOnCheckedChangeListener((buttonView, isChecked) -> {
            mIsChecked = isChecked;
            if (isChecked) {
                showDialog();
                activateFingerprint();
            } else {
                SharedPrefs.clearFingerprintPin();
            }
        });
        changePinButton.setClickedListener(v -> {
            String pin = pinInputView.getText().toString();
            SharedPrefs.setPin(pin);
            fingerprintSwitchView.setChecked(false);
            currentPinView.setText("Current PIN is = " + pin);
        });
    }

    private void initViews() {
        currentPinView = (Text) findComponentById(ResourceTable.Id_currentPinView);
        pinInputView = (TextField) findComponentById(ResourceTable.Id_pinInputView);
        changePinButton = findComponentById(ResourceTable.Id_changePinButton);
        fingerprintSwitchView = (SwitchCompat) findComponentById(ResourceTable.Id_fingerprintSwitchView);
        logoutButton = findComponentById(ResourceTable.Id_logoutButton);
        tvResult = (Text) findComponentById(ResourceTable.Id_result);
        currentPinView.setText("Current PIN is = " + SharedPrefs.getPin());
        fingerprintSwitchView.setChecked(SharedPrefs.getFingerprintPin() != null);
        changePinButton.setEnabled(false);
        fingerprintSwitchView.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                mIsChecked = !mIsChecked;
                fingerprintSwitchView.setChecked(mIsChecked);
            }
        });
    }

    private void showDialog() {
        dialog = new ProgressDialog(this);
        dialog.show();
    }

    private void dismissDialog() {
        dialog.hide();
    }
}
